import {Store, Getter, Mutation, Action} from 'decorators/vuex'
import { contains } from '@/utils/commonMethod/method.js'
@Store
export class ProductCopyStore {
  // 存model集合
  copyProductSelectFilter = {}

  // 存value集合
  copyProductSelectValue = {}

  copyProductCurrentSuperId = ''

  copyProductCurrentSelects = []

  copyProductBrandLetters = {}

  @Getter
  copyProductBrandLetters(state) {
    return state.copyProductBrandLetters
  }

  @Getter
  copyProductSelectFitler(state) {
    return state.copyProductSelectFilter
  }

  @Getter
  copyProductSelectValue (state) {
    return state.copyProductSelectValue
  }

  @Getter
  copyProductCurrentSelects(state) {
    return state.copyProductCurrentSelects
  }

  @Getter
  copyProductCurrentSuperId(state) {
    return state.copyProductCurrentSuperId
  }

  @Mutation
  setCopyProductSelectValue (state, selectValue) {
    state.copyProductSelectValue = selectValue
  }

  @Mutation
  setCopyProductBrandLetters (state, letters) {
    state.copyProductBrandLetters = letters
  }

  @Mutation
  setCopyProductCurrentSelects (state, selectItems) {
    state.copyProductCurrentSelects = selectItems
  }

  @Mutation
  setCopyProductSelectFilter (state, selectFilter) {
    state.copyProductSelectFilter = selectFilter
  }

  @Mutation
  setCopyProductCurrentSuperId (state, superId) {
    let arr = state.copyProductSelectFilter[superId]
    if (!arr) {
      state.copyProductSelectFilter[superId] = []
      state.copyProductSelectValue[superId] = []
    }

    state.copyProductCurrentSuperId = superId
  }

  @Action clearCopyProductSelect ({commit}) {
    commit('setCopyProductSelectFilter', [])
    commit('setCopyProductCurrentSuperId', '')
    commit('setCopyProductSelectValue', {})
    commit('setCopyProductCurrentSuperId', '')
  }

  @Action
  changeCopyProductCurrentSuperId ({commit}, superId) {
    commit('setCopyProductCurrentSuperId', superId)
  }

  @Action selectCopyProductFilterItem ({state}, item) {
    // 通过superId 找到对应的已选中数组
    const superIdSelectArr = state.copyProductSelectFilter[state.copyProductCurrentSuperId]
    const superIdSelectValue = state.copyProductSelectValue[state.copyProductCurrentSuperId]

    // 判断选中的是否已存在
    if (contains(superIdSelectArr, item)) {
      for (let i in superIdSelectArr) {
        if (superIdSelectArr[i] === item) {
          superIdSelectArr.splice(i, 1)
          superIdSelectValue.splice(i, 1)
          state.copyProductSelectFilter[state.copyProductCurrentSuperId] = superIdSelectArr
          state.copyProductSelectValue[state.copyProductCurrentSuperId] = superIdSelectValue
          return
        }
      }
    } else {
      superIdSelectArr.push(item)
      if (item && item.value) {
        superIdSelectValue.push(item.value)
      }
      state.copyProductSelectValue[state.copyProductCurrentSuperId] = superIdSelectValue
      state.copyProductSelectFilter[state.copyProductCurrentSuperId] = superIdSelectArr
    }
  }

  @Action getCopyProductSelectBySuperId({commit, state}, superId) {
    let arr = state.copyProductSelectFilter[superId]
    commit('setCopyProductCurrentSelects', arr)
  }
}
